package com.kim.useroauth.server.controller;

import com.kim.common.utilcom.model.Result;
import com.kim.sms.utils.AliyunSmsUtils;
import com.kim.sms.utils.KkbUtils;
import com.kim.useroauth.common.dto.PhoneCodeDTO;
import com.kim.useroauth.server.entity.UserAccount;
import com.kim.useroauth.server.service.IUserAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/app/v1/phone")
public class PhoneController {

    @Resource
    private IUserAccountService userAccountService;

    /**
     * redis
     */
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @PostMapping("/code")
    public Result code(@RequestBody @Valid PhoneCodeDTO dto) {
        //验证手机号是否已经注册过
        UserAccount userAccount = userAccountService.getUserAccount(dto.getPhone());
        if (userAccount == null) {
            return Result.failed(500, "手机号未注册");
        }
        //获取验证码
        String code = KkbUtils.sendCode("15337197832");
        //将验证码存入redis
        redisTemplate.opsForValue().set("sms_code" + ":" + dto.getPhone(), code, 60 * 5, TimeUnit.SECONDS);
        return Result.succeed("短信发送成功");
    }


}
